package com.ty.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.ty.dto.CanalDto;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Component
public class Consumer {

    @Autowired
    private ObjectMapper objectMapper;

    @RabbitListener(queues = "stock.update.queue")
    public void handleMessage(CanalDto message) {

        Long refid = message.getId();

        List<HashMap<String, Object>> datelist=(List<HashMap<String, Object>>) message.getData();

           /* orderMasterDao.OrderItem orderItem = new orderMasterDao.OrderItem();
            if (datelist!=null && datelist.isEmpty()){
                for (HashMap<String, Object> map : datelist){
                    orderItem.setId((long) Integer.parseInt(map.get("id").toString()));
                    orderItem.setOrderId((long) Integer.parseInt(map.get("orderId").toString()));
                    orderItem.setProductId(Integer.parseInt(map.get("productId").toString()));
                    orderItem.setProductName(map.get("productName").toString());
                    orderItem.setImg(Collections.singletonList(map.get("Img").toString()));
                    orderItem.setQty(Integer.parseInt(map.get("qty").toString()));
                    orderItem.setPrice(BigDecimal.valueOf(Long.parseLong((map.get("price").toString()))));
                }
            }*/

      /*  try {
            InvertoryLog payload = objectMapper.readValue(message, InvertoryLog.class);
            String productId = String.valueOf(payload.getRefId());
            Pattern pattern = Pattern.compile("\\d+");
            Matcher orderId =pattern.matcher(productId);
            Integer status = 110;

            Integer operationType = payload.getOptionType();
            Integer qty = payload.getQty();

            switch (operationType) {
                case 0 :
                    System.out.println("没有发生改变，不执行任何操作");
                    break;
                case 90 :
                    if (status.equals(110)){
                        System.out.println("用户下单，但未在规定时间内支付，不执行任何操作");
                    }else {
                        System.out.println("用户下单成功,销量增加,销量为:"+qty);
                    }

                    break;

                default:
                    break;
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
*/

    }

}
